import { Partial } from '@/types/global';
import { LoginResult } from '@/types/member'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<LoginResult>();

    // 保存会员信息，登录时使用
    const setProfile = (value: Partial<LoginResult>) => {
      if (profile.value) {
        Object.assign(profile.value, value);
      } else {
        profile.value = value as LoginResult;
      }
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = void 0;
    }

    // 记得 return
    return { profile, setProfile, clearProfile };
  },
  {
    // 适配多端的持久化配置
    persist: {
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
    },
  },
)
